package cn.hnucm.handler;

import cn.hnucm.service.LoginService;
import cn.hnucm.utils.Result;
import cn.hnucm.utils.SpringUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * @Author:liweihua
 * @Description:    登录成功的处理器
 * @Date: 11:27$ 2021/12/18/018$
 * @Modified:
 */
public class SuccessHandler implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authentication) throws IOException, ServletException {
        AuthenticationSuccessHandler.super.onAuthenticationSuccess(request, response, chain, authentication);
    }

    //登录成功逻辑
    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
//        //登录成功返回
//        LoginService loginService = SpringUtils.getBean("loginService", LoginService.class);
//        System.out.println("******************"+ loginService);
//        response.setCharacterEncoding("UTF-8");
////在响应中主动告诉浏览器使用UTF-8编码格式来接收数据
//        response.setHeader("Content-Type", "application/json;charset=UTF-8");
////可以使用封装类简写Content-Type，使用该方法则无需使用setCharacterEncoding
//        response.setContentType("application/json;charset=UTF-8");
//        Map<String, Object> map = loginService.verifyUser();
//        //设置返回请求头
//        response.setContentType("application/json;charset=utf-8");
//        //写出流
//        PrintWriter out = response.getWriter();
//        out.write(JSONObject.toJSONString(Result.ok(map)));
//        out.flush();
//        out.close();
    }
}
